<template>
	<block>
		<view id="navbar">
			<u-navbar title-size="32" title-color="#000" bgColor="unset" height="44" @leftClick="leftClick">
				<view class="u-nav-slot" slot="left">
					<u-icon name="home" size="20" v-if="isHome"></u-icon>
					<u-icon name="arrow-left" size="19" v-else></u-icon>
				</view>
			</u-navbar>
		</view>
		<scroll-view style="height: calc(100vh - 1px);" scroll-y v-if="firstShow&&!isSubmit" :scroll-top="scrollTop"
			@scroll="onScroll" :scroll-into-view="scrollIntoViewId">
			<div id="img-bg"></div>
			<div class="content">
				<div id="bg" v-if="info">
					<div class="title">{{info.title}}</div>
					<div class="desc" v-if="info.desc">
						<rich-text :nodes="info.desc"></rich-text>
					</div>
				</div>
				<block v-for="(page, index) in pageSubjects" :key="index">
					<view v-show="page.show">
						<view v-for="subject in page.subjects" :key="subject.SUBJECT_ID"
							:id="`subject${subject.SUBJECT_ID}`" :class="{noEvent: isDisabled}">
							<subject-render :real-subject="subject"
								v-show="subject.skipShow && subject.subjectRelationShow" @radioChange="radioChange"
								@checkboxCiteFn="checkboxCiteFn" @subjectRealtionChange="subjectRealtionChange"
								@subjectOptionRealtionChange="subjectOptionRealtionChange" />
						</view>
					</view>
				</block>
				<div class="btn" v-if="isShowSubmitBtn" @click="onSubmit">
					提交问卷
				</div>
				<div class="btn" v-if="isShowNextBtn" @click="nextPage">
					下一页
				</div>
				<div class="plain-btn" v-if="isShowReturnPrevPageBtn" @click="prevPage">返回上一页</div>
				<div class="placeholder"></div>
			</div>
		</scroll-view>
<!-- 		<div v-if="isSubmit" class="submit">
			<div style="height: 100rpx;"></div>
			<div class="submit-evalute"> <u-icon name="checkmark-circle-fill" color="#78FFE6"></u-icon>
				<text>感谢您的参与！</text>
			</div>
			<div class="submit-thank" v-if="questionnaire" style="padding: 0 32rpx;">
				<u-parse v-if="questionnaire.DEFINED_OVER_MSG" :content="questionnaire.DEFINED_OVER_MSG"></u-parse>
				<div v-else>感谢您对青丝几何的支持</div>
			</div>
			<div class="submit-point" v-if="point">
				奖励{{ point }}积分已到账，注意查收哦
			</div>
			<div class="submit-point" style="padding: 0 14px" v-else-if="reward.name">感谢您的参与，青丝几何<span
					v-if="reward.url == `/pages_index/coupon/index?activeIndex=2`">【</span>{{
		    reward.name
		  }}<span v-if="reward.url == `/pages_index/coupon/index?activeIndex=2`">】</span>已发放至您的账户，可在青丝几何会员中心查看</div>
			<div class="submit-back__home" v-if="!reward" @click="goHome">返回首页</div>
			<div class="submit-back__home" v-if="reward" @click="goLook">去看看</div>
			<div class="submit-look__more" @click="goHfs">查看归因分析</div>
			<Survey :surveyList="list" v-if="list.length > 0" />
			<div v-for="(item, index) in advertisement_list" :key="index" v-show="advertisement_list.length > 0"
				@click="goDel(item)"
				style="border-radius: 8rpx;display: flex;align-items: center;justify-content: center;margin: 40rpx auto;position: relative;">
				<img mode="widthFix" :src="item.img_url" alt="" style="width: 654rpx;">
			</div>

		</div> -->
		<u-loading-page :loading="loading" loading-text="加载问卷中..."></u-loading-page>
	</block>
</template>
<script>
	import Survey from "../components/survey/index.vue"
	import {
		wx_questionnaire_list,
		wx_questionnaire_submit,
		wx_get_questionnaire_advertising,
		wx_questionnaire_sel
	} from "@/api"
	// import {
	// 	navTo,
	// } from "@/common/utils/uni_app";
	import {
		composeFns,
		convertNewlinesToBr
	} from '@/utils'
	import {
		subjectMap,
		partiallyShuffleArray
	} from './subjectFn'
	import {
		mapActions,
		mapState
	} from 'vuex'
	import subjectRender from './subjectRender'
	export default {
		data() {
			return {
				advertisement_list: [],
				historyLength: 0,
				info: null,
				firstShow: false,
				pageHistory: [],
				currentPageIndex: 0,
				scrollTop: 0,
				oldScrollTop: 0,
				isSubmitLock: false,
				isSubmit: false,
				point: null,
				reward: null,
				question_id: null,
				scrollIntoViewId: null,
				isDisabled: false,
				pageNumbers: [],
				loading: true,
				isPreview: false,
				cp: false,
				result_id: null,
				list: [],
				questionnaire: null
			}
		},
		components: {
			subjectRender,
			Survey
		},
		computed: {
			...mapState('subject', ['pageSubjects']),
			isHome() {
				return this.historyLength <= 1
			},
			isShowSubmitBtn() {
				if (this.isPreview) return false
				if (this.isDisabled) return false
				if (!this.pageSubjects.length) return false
				if (this.pageSubjects.length == 1) {
					return true
				}
				if (this.pageSubjects[this.pageSubjects.length - 1].show) {
					return true
				}
				const skipShowsubjects = this.pageSubjects[this.currentPageIndex].subjects.filter(s => s.skipShow)
				for (const s of skipShowsubjects) {
					if (s.SKIP_SUBJECT_ID == -2) return true
				}
				for (const s of skipShowsubjects) {
					if (s.SUBJECT_TYPE == 10) {
						// 只有单选 有选项跳题 存在选项终结提交
						if (s.payload.value) {
							const option = s.options.find(o => o.ID == s.payload.value)
							if (option.SKIP && option.SKIP == -2) {
								return true
							}
						}
					}
				}
				return false
			},
			isShowNextBtn() {
				if (!this.pageSubjects.length) return false
				if (this.pageSubjects.length == 1) {
					return false
				}
				if (this.isShowSubmitBtn) {
					return false
				}
				if (this.pageSubjects.length - 1 == this.currentPageIndex) {
					return false
				}
				return true
			},
			isShowReturnPrevPageBtn() {
				if (!this.pageSubjects.length) return false
				if (this.pageHistory.length > 1) {
					return true
				}
				return false
			}
		},
		methods: {
			...mapActions('subject', ['updatePageSubjects', 'updatePageShow']),
			async wx_questionnaire_submit(items) {
				const payload = {
					items,
					question_id: Number(this.question_id)
				}
				if (this.from_no) {
					payload.from_no = this.from_no
				}
				if (this.from_type) {
					payload.from_type = this.from_type
				}
				const res = await wx_questionnaire_submit(payload)
				return res
			},
			goDel(item) {
				// if (item.type == 2) {
				// 	navTo(item.jump_url)
				// }
				if (item.type == 3) {
					wx.navigateTo({
						url: '/pages_index/webView/index?url=' + item.jump_url
					});
				}
			},
			async wx_get_questionnaire_advertising() {
				const data = await wx_get_questionnaire_advertising({
					question_id: this.question_id
				})
				if (data.advertisement_list && data.advertisement_list.length > 0) {
					this.advertisement_list = data.advertisement_list
				}
			},
			goLook() {
				wx.navigateTo({
					url: this.reward.url,
				});
			},
			goHome() {
				wx.switchTab({
					url: "/pages/home",
				});
			},
			goHfs() {
				// wx.navigateTo({
				// 	url: "/pages_index/causeAnalysis/index?result_id=" + this.result_id
				// })
				
				wx.redirectTo({
					url: "/pages_index/causeAnalysis/index?result_id=" + this.result_id
				})
			},
			checkCurrentPageValue() {
				const subjects = this.pageSubjects[this.currentPageIndex].subjects.filter(s => s.skipShow && s
					.subjectRelationShow)
				for (const s of subjects) {
					const {
						value,
						subject
					} = this[`subjectFn[${s.SUBJECT_TYPE}]`](s)
					if (s.IS_ANSWER == 0) continue
					console.log(s, value, subject, 'subject')
					if (!value) {
						subject.unAnswer = true
						this.scrollIntoViewId = `subject${s.SUBJECT_ID}`
						this.$nextTick(() => {
							this.scrollIntoViewId = null
						})
						return false
					}
					if (s.SUBJECT_TYPE == 11 && s.OPTION_MIN && s.OPTION_MIN > 1) {
						if (s.OPTION_MIN > value.length) {
							subject.unAnswer = true
							this.scrollIntoViewId = `subject${s.SUBJECT_ID}`
							this.$nextTick(() => {
								this.scrollIntoViewId = null
							})
							return false
						}
					}
					if (s.SUBJECT_TYPE == 12 && s.OPTION_MIN && s.OPTION_MIN > 1) {
						if (s.OPTION_MIN > value.answer_content.length) {
							subject.unAnswer = true
							this.scrollIntoViewId = `subject${s.SUBJECT_ID}`
							this.$nextTick(() => {
								this.scrollIntoViewId = null
							})
							return false
						}
					}
					if (s.SUBJECT_TYPE == 13 && s.OPTION_MIN && s.OPTION_MIN > 1) {
						if (s.OPTION_MIN > s.payload.examPic.length) {
							subject.unAnswer = true
							this.scrollIntoViewId = `subject${s.SUBJECT_ID}`
							this.$nextTick(() => {
								this.scrollIntoViewId = null
							})
							return false
						}
					}
				}
				return true
			},
			getAllValue() {
				const list = []
				for (const p of this.pageSubjects) {
					for (const s of p.subjects) {
						if (s.skipShow && s.subjectRelationShow) {
							const {
								value
							} = this[`subjectFn[${s.SUBJECT_TYPE}]`](s)
							if (Array.isArray(value)) {
								value.forEach(v => list.push(v))
							} else if (value) {
								list.push(value)
							}
						}
					}
				}
				return list
			},
			async onSubmit() {
				if (this.checkCurrentPageValue()) {
					const list = this.getAllValue()
					if (this.isSubmitLock) return
					this.isSubmitLock = true
					console.log('拿到答案', list)
					const res = await this.wx_questionnaire_submit(list)
					this.isSubmitLock = false
					this.isSubmit = true
					this.result_id = res.result_id
					this.goHfs()
				}
			},
			subjectOptionRealtionChange({
				sonList,
				subject
			}) {

				const pageSubjects = JSON.parse(JSON.stringify(this.pageSubjects))
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						if (sonList.includes(s.SUBJECT_ID)) {
							s.options.forEach(o => {
								if (o.OPTIONS1_ASSOCIATION && o.OPTIONS1_ASSOCIATION.length) {
									o.OPTIONS1_ASSOCIATION.filter(a => a.ASSOCIATION_SUBJECT_ID == subject
										.SUBJECT_ID).forEach(a => {
										// 单选
										if (subject.SUBJECT_TYPE == 10) {
											const sno = a.ASSOCIATION_SUBJECT_OPTION1_SNO
											if (subject.payload.value) {
												const fatherOption = subject.options.find(fo => fo
													.ID == subject.payload.value)
												if (sno == fatherOption.SNO) {
													a.show = true
												} else {
													a.show = false
												}
											}
										}
										// 多选
										if (subject.SUBJECT_TYPE == 11) {
											const snos = a.ASSOCIATION_SUBJECT_OPTION1_SNO.split(',').map(
												aSno => Number(aSno))
											if (a.SUBJECT_ASSOCIATION_TYPE == 1) {
												// 任意选项
												const fatherOptionsSno = subject.options.filter(fo =>
													subject
													.payload.value.includes(fo
														.ID)).map(fo => fo.SNO)
												a.show = snos.some(sno => fatherOptionsSno.includes(sno))
											}
											if (a.SUBJECT_ASSOCIATION_TYPE == 2) {

												// 选择全部选项
												const fatherOptionsSno = subject.options.filter(fo =>
													subject
													.payload.value.includes(fo
														.ID)).map(fo => fo.SNO)
												a.show = snos.every(sno => fatherOptionsSno.includes(sno))
											}
											if (a.SUBJECT_ASSOCIATION_TYPE == 3) {
												// 没有选择任意
												const fatherOptionsSno = subject.options.filter(fo =>
													subject
													.payload.value.includes(fo
														.ID)).map(fo => fo.SNO)
												a.show = !snos.some(sno => fatherOptionsSno.includes(sno))
											}
											if (a.SUBJECT_ASSOCIATION_TYPE == 4) {
												// 没有选择全部
												const fatherOptionsSno = subject.options.filter(fo =>
													subject
													.payload.value.includes(fo
														.ID)).map(fo => fo.SNO)
												a.show = !snos.every(sno => fatherOptionsSno.includes(sno))
											}
											if (s.OPTION_MAX) {
												if (s.payload.value.length >= s.OPTION_MAX) {

													if (s.payload.value.includes(o.ID) && !(o.citeShow && o
															.optionAssShow)) {
														const vIndex = s.payload.value.findIndex(v => v ===
															o.ID)
														s.payload.value.splice(vIndex, 1)
														s.options.forEach(sSon => sSon.disabled = false)
													}
												}
											}
										}

										// 打分星星
										if (subject.SUBJECT_TYPE == 29) {
											const sno = a.ASSOCIATION_SUBJECT_OPTION1_SNO
											if (subject.payload.value) {
												if (sno == subject.payload.value) {
													a.show = true
												} else {
													a.show = false
												}
											}
										}

										// NFS
										if (subject.SUBJECT_TYPE == 27) {
											const sno = a.ASSOCIATION_SUBJECT_OPTION1_SNO
											if (subject.payload.value) {
												if (Number(subject.payload.value) >= Number(sno)) {
													a.show = true
												} else {
													a.show = false
												}
											}
										}
									})
									let arrFn = 'every'
									if (o.OPTION_ASSOCIATION_RELATIONSHIP == 2) {
										arrFn = 'some'
									}
									o.optionAssShow = o.OPTIONS1_ASSOCIATION[arrFn](s1 => s1.show)
								}
							})
						}
					}
				}
				this.updatePageSubjects(pageSubjects)
			},
			traverseAllSubjectSkipShowBySubjectRelationShow() {
				const pageSubjects = JSON.parse(JSON.stringify(this.pageSubjects))
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						if (!s.subjectRelationShow) {
							if (s.SKIP_SUBJECT_ID == -3) {
								// 选项跳题
								s.options.forEach(o => {
									if (o.SKIP != -1) {
										this.innerTraverse(Number(o.SKIP), pageSubjects)
									}
								})

							} else if (s.SKIP_SUBJECT_ID != -2 && s.SKIP_SUBJECT_ID != -1) {
								// 跨题目跳题
								this.innerTraverse(s.SKIP_SUBJECT_ID, pageSubjects)
							}
						}
					}
				}
				this.updatePageSubjects(pageSubjects)
			},
			innerTraverse(sno, pageSubjects) {
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						if (s.SUBJECT_SNO == sno) {
							s.skipShow = false
						}
					}
				}
			},
			subjectRealtionChange({
				sonList,
				subject
			}) {
				const pageSubjects = JSON.parse(JSON.stringify(this.pageSubjects))
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						if (sonList.includes(s.SUBJECT_ID)) {
							const ass = s.SUBJECT_ASSOCIATION.find(a => a.ASSOCIATION_SUBJECT_ID == subject.SUBJECT_ID)
							if (ass) {
								const snoList = ass.ASSOCIATION_SUBJECT_OPTION1_SNO.split(',').map(n => Number(n))
								// 单选 nps 打分星星 只能是选择任意选项
								// 多选 4 种 规则都支持
								if (subject.SUBJECT_TYPE == 10) {
									// 单选
									const option = subject.options.find(o => o.ID == subject.payload.value)
									if (option) {
										ass.show = snoList.some(sno => sno == option.SNO)
									} else {
										ass.show = false
									}
								}
								if (subject.SUBJECT_TYPE == 11) {
									const optionSnos = subject.options.filter(o => subject.payload.value.includes(o.ID))
										.map(o => o.SNO)
									// 多选
									if (ass.SUBJECT_ASSOCIATION_TYPE == 1) {
										if (optionSnos.length) {
											ass.show = snoList.some(sno => optionSnos.includes(sno))
										} else {
											ass.show = false
										}
									}
									if (ass.SUBJECT_ASSOCIATION_TYPE == 2) {
										if (optionSnos.length) {
											ass.show = snoList.every(sno => optionSnos.includes(sno))
										} else {
											ass.show = false
										}
									}
									if (ass.SUBJECT_ASSOCIATION_TYPE == 3) {
										ass.show = !snoList.some(sno => optionSnos.includes(sno))
									}
									if (ass.SUBJECT_ASSOCIATION_TYPE == 4) {
										ass.show = !snoList.every(sno => optionSnos.includes(sno))
									}
								}
								if (subject.SUBJECT_TYPE == 27) {
									// nps
									ass.show = snoList.some(sno => sno == subject.payload.value)
								}
								if (subject.SUBJECT_TYPE == 29) {
									// 打分星星
									ass.show = snoList.some(sno => sno == subject.payload.value)
								}
								// 根据所有条件去判断显示
								let arrFn = 'every'
								if (s.SUBJECT_ASSOCIATION_RELATIONSHIP == 2) {
									arrFn = 'some'
								}
								// 父题目有跳题显示才走 否则强制不显示
								s.subjectRelationShow = s.SUBJECT_ASSOCIATION[arrFn](ASSOCIATION => ASSOCIATION.show)
							}

						}

					}
				}
				this.updatePageSubjects(pageSubjects)
				setTimeout(this.traverseAllSubjectSkipShowBySubjectRelationShow)
			},
			checkboxCiteFn({
				subject
			}) {
				const pageSubjects = JSON.parse(JSON.stringify(this.pageSubjects))
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						if (s.CITATION_SNO && s.CITATION_SNO == subject.SUBJECT_SNO) {
							const snoList = subject.options.filter(o => subject.payload.value.includes(o.ID)).map(o => o
								.SNO)
							s.options.forEach(o => {
								if (snoList.includes(o.SNO)) {
									o.citeShow = true
								} else {
									o.citeShow = false
								}
							})
						}
					}
				}
				this.updatePageSubjects(pageSubjects)
			},
			// 根据当前题目, 重置后面所有skipShow显示
			resetCurrentRestSubjectSkipShow(subject) {
				const id = subject.SUBJECT_ID
				const pageSubjects = JSON.parse(JSON.stringify(this.pageSubjects))
				let nextShow = true
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						if (!nextShow) {
							s.skipShow = nextShow
						}
						if (s.SUBJECT_ID == id) {
							nextShow = false
						}
					}
				}
				this.updatePageSubjects(pageSubjects)
			},

			// skip number类型
			radioChange({
				subject,
				option,
				skip
			}) {
				this.resetCurrentRestSubjectSkipShow(subject)
				if (option) {
					// 选项跳题
				} else {
					// 跳题
				}
				if (skip != -2) {
					const pageSubjects = JSON.parse(JSON.stringify(this.pageSubjects))
					// 这里没有-3的情况
					if (skip == -1) {
						let cacheSubject = null
						let startGetSubject = false
						for (const p of pageSubjects) {
							for (const s of p.subjects) {
								if (startGetSubject) {
									cacheSubject = s
									startGetSubject = false
								}
								if (s.SUBJECT_ID == subject.SUBJECT_ID) {
									startGetSubject = true
								}
							}
						}
						if (cacheSubject) {
							this.startDealRestSkipShow(cacheSubject)
						}
						return
					}

					for (const p of pageSubjects) {
						for (const s of p.subjects) {
							if (s.SUBJECT_SNO == skip) {
								this.startDealRestSkipShow(s)
								return
							}
						}
					}
				} else if (skip == -2) {
					const pageSubjects = JSON.parse(JSON.stringify(this.pageSubjects))
					let start = false
					for (const p of pageSubjects) {
						for (const s of p.subjects) {
							if (start) {
								s.skipShow = false
							}
							if (s.SUBJECT_ID == subject.SUBJECT_ID) {
								start = true
							}
						}
					}
					this.updatePageSubjects(pageSubjects)
				}
			},
			singleSkipShow(s, pageSubjects) {
				for (const p of pageSubjects) {
					for (const subject of p.subjects) {
						if (subject.SUBJECT_ID == s.SUBJECT_ID) {
							subject.skipShow = true
						}
					}
				}
			},
			// 开始处理剩余skipShow显示
			startDealRestSkipShow(currentSubject) {
				const pageSubjects = JSON.parse(JSON.stringify(this.pageSubjects))
				const inner = (s) => {
					this.singleSkipShow(s, pageSubjects)
					// 这里跟初始化并不相同,单选题有可能已经有缓存选项,既然有选项,就有可能有逻辑
					if (s.SKIP_SUBJECT_ID == -2 || s.SKIP_SUBJECT_ID == -3) {
						// -3 只会在单选题存在, s.payload.value 存在代表有缓存选项,需要走逻辑
						if (s.SKIP_SUBJECT_ID == -3 && s.payload.value) {
							setTimeout(() => {
								const option = s.options.find(o => o.ID == s.payload.value)
								this.radioChange({
									subject: s,
									option,
									skip: Number(option.SKIP)
								})
							}, 0)
						}

					}

					if (s.SKIP_SUBJECT_ID != -1) {
						// 跳题
						for (const p of pageSubjects) {
							for (const subject of p.subjects) {
								if (subject.SUBJECT_SNO == s.SKIP_SUBJECT_ID) {
									inner(subject)
								}
							}
						}
					}

					if (s.SKIP_SUBJECT_ID == -1) {
						let cacheSubject = null
						let startGetSubject = false
						for (const p of pageSubjects) {
							for (const subject of p.subjects) {
								if (startGetSubject) {
									cacheSubject = subject
									startGetSubject = false
								}
								if (s.SUBJECT_ID == subject.SUBJECT_ID) {
									startGetSubject = true
								}
							}
						}
						if (cacheSubject) {
							inner(cacheSubject)
						}
					}
				}

				inner(currentSubject)
				this.updatePageSubjects(pageSubjects)
			},
			onScroll(e) {
				this.oldScrollTop = e.detail.scrollTop
			},
			setScrollTop(scrollTop = 0) {
				this.scrollTop = this.oldScrollTop
				this.$nextTick(() => {
					this.scrollTop = scrollTop
				})
			},
			nextPage() {
				if (this.isDisabled) {
					this.currentPageIndex = this.pageNumbers[this.pageHistory.length] - 1
					this.updatePageShow(this.currentPageIndex)
					this.pageHistory.push(this.currentPageIndex)
					this.setScrollTop()
					return
				}
				if (!this.checkCurrentPageValue()) return
				const subjects = this.pageSubjects[this.currentPageIndex].subjects.filter(s => s.skipShow)
				if (subjects.some(s => s.SKIP_SUBJECT_ID == -2)) return
				if (subjects.every(s => s.SKIP_SUBJECT_ID == -1)) {
					this.currentPageIndex++
				} else {
					for (let i = subjects.length - 1; i >= 0; i--) {
						const subject = subjects[i]
						if (subject.SKIP_SUBJECT_ID == -3) {
							// 选项跳题 单选
							const option = subject.options.find(o => o.ID == subject.payload.value)
							if (option.SKIP != -1) {
								const skipSubject = subjects.find(s => s.SUBJECT_SNO == option.SKIP)
								if (skipSubject) {
									// 跳题 跳到当页代表可以直接下一页
									this.currentPageIndex++
								} else {
									// 不在当前页面那肯定在其他页面
									for (const p of this.pageSubjects) {
										for (const s of p.subjects) {
											if (s.SUBJECT_SNO == option.SKIP) {
												this.currentPageIndex = s.PAGE_NUMBER - 1
											}
										}
									}
								}
							}
							if (option.SKIP == -1) {
								this.currentPageIndex++
							}
							break
						}
						if (subject.SKIP_SUBJECT_ID != -1 && subject.SKIP_SUBJECT_ID != -3) {
							// 直接跳题
							const skipSubject = subjects.find(s => s.SUBJECT_SNO == subject.SKIP_SUBJECT_ID)
							if (skipSubject) {
								this.currentPageIndex++
							} else {
								for (const p of this.pageSubjects) {
									for (const s of p.subjects) {
										if (s.SUBJECT_SNO == subject.SKIP_SUBJECT_ID) {
											this.currentPageIndex = s.PAGE_NUMBER - 1
										}
									}
								}
							}
							break
						}
					}
				}
				this.updatePageShow(this.currentPageIndex)
				this.pageHistory.push(this.currentPageIndex)
				this.setScrollTop()
			},
			prevPage() {
				this.pageHistory = this.pageHistory.slice(0, this.pageHistory.length - 1)
				this.currentPageIndex = this.pageHistory[this.pageHistory.length - 1]
				this.updatePageShow(this.currentPageIndex)
				this.setScrollTop()
			},
			leftClick() {
				if (this.isHome) {
					wx.switchTab({
						url: '/pages/index/index'
					})
					return
				}
				wx.navigateBack({
					delta: 1
				})
			},
			getCurrentPages(options) {
				const pages = getCurrentPages()
				this.historyLength = pages.length
			},
			initSubjectShow(pageSubjects) {
				// SKIP_SUBJECT_ID
				// -1 直接下一题
				// -2 直接结束
				// -3 选项跳题(单选题)
				// 其他跳转的题号

				// -2 或者 -3 直接所有后续全部false
				let isReturn = false
				// 不是-1 -2 -3 直接跳题
				let skipNo = null
				for (const p of pageSubjects) {
					p.subjects.forEach(s => {
						innerCheckSingleSubject(s)
					})
				}

				function innerCheckSingleSubject(subject) {
					if (isReturn) {
						subject.skipShow = false
						return
					}
					if (!skipNo) {
						if (subject.SKIP_SUBJECT_ID == -2 || subject.SKIP_SUBJECT_ID == -3) {
							subject.skipShow = true
							isReturn = true
							return
						}

						if (subject.SKIP_SUBJECT_ID != -1) {
							subject.skipShow = true
							skipNo = subject.SKIP_SUBJECT_ID
							return
						}

						if (subject.SKIP_SUBJECT_ID == -1) {
							subject.skipShow = true
							return
						}
						return
					}

					if (subject.SUBJECT_SNO == skipNo) {
						skipNo = null
						innerCheckSingleSubject(subject)
					} else {
						subject.skipShow = false
					}
				}
				return pageSubjects
			},
			initBaseSubject(pageSubjects) {
				for (const p of pageSubjects) {
					p.subjects.forEach(s => {
						const {
							type,
							name,
							setPayload,
							setOptions,
							setTicks,
							setNfs,
							setMatrixNfs,
							setCheckboxMax,
							setMultpleScore
						} = subjectMap[s.SUBJECT_TYPE]
						s.type = type
						s.payload = setPayload && setPayload(s)
						s.typeName = name
						try {
							s.SUBJECT_NAME = s.SUBJECT_NAME.replace(/\[&product&\]/g, this.info.PRODUCT_NAME)
						} catch (e) {
							//TODO handle the exception
						} finally {
							s.title = s.SUBJECT_NAME
						}
						s.is_require = s.IS_ANSWER == 1
						s.unAnswer = false
						setOptions && s.options.forEach(option => setOptions(option))
						setTicks && (s.ticks = setTicks(s))
						setNfs && (s.nfs = setNfs(s))
						setMatrixNfs && setMatrixNfs(s)
						setCheckboxMax && setCheckboxMax(s)
						setMultpleScore && setMultpleScore(s)
					})
				}
				return pageSubjects
			},
			initRandomSubject(pageSubjects) {
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						// 单选、多选、下拉单选才有乱序选项
						if ([10, 11, 25].includes(Number(s.SUBJECT_TYPE)) && s.SHOW_COLLATION != -1) {
							s.options = partiallyShuffleArray(s.options, s.SHOW_COLLATION)
						}
					}
				}
				return pageSubjects
			},
			initCiteShow(pageSubjects) {
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						if (s.CITATION_SNO) {
							s.options.forEach(o => o.citeShow = false)
						} else {
							s.options && Array.isArray(s.options) && s.options.forEach(o => o.citeShow = true)
						}
					}
				}
				return pageSubjects
			},
			initSubjectRelationShow(pageSubjects) {
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						if (s.SUBJECT_ASSOCIATION && s.SUBJECT_ASSOCIATION.length) {
							s.subjectRelationShow = false
						} else {
							s.subjectRelationShow = true
						}
					}
				}
				return pageSubjects
			},
			initSubjectRelationFather(pageSubjects) {
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						if (s.SUBJECT_ASSOCIATION && s.SUBJECT_ASSOCIATION.length) {
							s.SUBJECT_ASSOCIATION.forEach(a => {
								a.show = false
								this.singleRelationFather(pageSubjects, a)
							})
						}
					}
				}
				return pageSubjects
			},
			singleRelationFather(pageSubjects, a) {
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						if (a.ASSOCIATION_SUBJECT_SNO == s.SUBJECT_SNO) {
							s.isSonRelation = true
							if (!s.sonRelationSubjectIdList && !Array.isArray(s.sonRelationSubjectIdList)) {
								s.sonRelationSubjectIdList = []
							}
							if (!s.sonRelationSubjectIdList.includes(a.SUBJECT_ID)) {
								s.sonRelationSubjectIdList.push(a.SUBJECT_ID)
								a.ASSOCIATION_SUBJECT_ID = s.SUBJECT_ID
							}
						}
					}
				}
			},
			initSubjectOptionRelationShow(pageSubjects) {
				// 只存在单选 多选才会触发
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						if ([10, 11].includes(Number(s.SUBJECT_TYPE))) {
							s.options.forEach(o => {
								if (o.OPTIONS1_ASSOCIATION && o.OPTIONS1_ASSOCIATION.length) {
									o.optionAssShow = false
									o.OPTIONS1_ASSOCIATION.forEach(ass => {
										ass.show = false
										this.singleOptionRelationFather(pageSubjects, ass, s)
									})
								} else {
									o.optionAssShow = true
								}
							})
						}
					}
				}
				return pageSubjects
			},
			singleOptionRelationFather(pageSubjects, a, subject) {
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						if (a.ASSOCIATION_SUBJECT_SNO == s.SUBJECT_SNO) {
							s.isSonOptionRelation = true
							if (!s.sonOptionRelationSubjectIdList && !Array.isArray(s.sonOptionRelationSubjectIdList)) {
								s.sonOptionRelationSubjectIdList = []
							}
							if (!s.sonOptionRelationSubjectIdList.includes(subject.SUBJECT_ID)) {
								s.sonOptionRelationSubjectIdList.push(subject.SUBJECT_ID)
							}
							a.ASSOCIATION_SUBJECT_ID = s.SUBJECT_ID
						}
					}
				}
			},
			initSubjectGetValueFn(pageSubjects) {
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						// 这里因为vue2的数据响应式原因,把函数放在this指针上
						this[`subjectFn[${s.SUBJECT_TYPE}]`] = subjectMap[s.SUBJECT_TYPE].getSubjectValue
					}
				}
				return pageSubjects
			},
			async wx_questionnaire_list(question_id, is_preview = null) {
				const {
					questionnaires
				} = await wx_questionnaire_list({
					question_id,
					is_preview
				})
				this.initData(questionnaires)
				setTimeout(() => {
					this.loading = false
				}, 1000)
			},
			initData(questionnaires, items) {
				if (questionnaires.length) {
					const questionnaire = questionnaires[0]
					this.questionnaire = questionnaire
					this.info = {
						title: questionnaire.QUESTION_TITLE,
						desc: questionnaire.QUESTION_DESC ? convertNewlinesToBr(questionnaire.QUESTION_DESC) : '',
						PRODUCT_NAME: questionnaire.PRODUCT_NAME || ''
					}
					const pageLength = JSON.parse(questionnaire.PAGE_INFO).length
					const pageSubjects = []
					for (let i = 1; i <= pageLength; i++) {
						const subjects = questionnaire.subjects.filter(s => s.PAGE_NUMBER == i)
						pageSubjects.push({
							show: false,
							subjects,
							page: i
						})
					}
					const compose = composeFns(this.initSubjectShow, this.initBaseSubject, this.initRandomSubject, this
						.initCiteShow, this.initSubjectRelationShow, this.initSubjectRelationFather, this
						.initSubjectOptionRelationShow, this.initSubjectGetValueFn)
					const list = compose(pageSubjects)
					if (this.isDisabled) {
						let pageNumbers = []
						// 当是查看问卷答案的情况,要把跳转的逻辑显示去掉
						for (const p of list) {
							for (const s of p.subjects) {
								if (items.find(item => s.SUBJECT_ID == item.subject_id)) {
									s.skipShow = true
									s.subjectRelationShow = true
									pageNumbers.push(s.PAGE_NUMBER)
								}
							}
						}
						pageNumbers = [...new Set(pageNumbers)]
						this.pageNumbers = pageNumbers
					}
					this.updatePageSubjects(list)
					this.firstShow = true
					// 首次首页要显示
					this.updatePageShow(this.currentPageIndex)
					this.pageHistory.push(0)
				}
			},
			setSubjectValue(items) {
				const pageSubjects = JSON.parse(JSON.stringify(this.pageSubjects))
				for (const p of pageSubjects) {
					for (const s of p.subjects) {
						const list = items.filter(item => item.subject_id == s.SUBJECT_ID)
						if (list.length) {
							if (s.SUBJECT_TYPE == 10) {
								s.payload.value = Number(list[0].option1)
								if (list[0].answer_content) {
									s.options.find(o => o.ID == s.payload.value).value = list[0].answer_content
								}
							}
							if (s.SUBJECT_TYPE == 11) {
								s.payload.value = list.map(item => Number(item.option1))
								list.forEach(item => {
									if (item.answer_content) {
										s.options.find(o => o.ID == item.option1).value = item.answer_content
									}
								})
							}
							if (s.SUBJECT_TYPE == 25) {
								s.payload.value = Number(list[0].option1)
								s.payload.text = s.options.find(o => o.ID == list[0].option1).OPTION_NAME
							}
							if (s.SUBJECT_TYPE == 12) {
								s.payload.value = list[0].answer_content
							}
							if (s.SUBJECT_TYPE == 26) {
								s.payload.value = Number(list[0].answer_content)
							}
							if (s.SUBJECT_TYPE == 27) {
								s.payload.value = Number(list[0].answer_content)
								s.nfs.forEach((t, index) => {
									if (index <= Number(list[0].answer_content)) {
										t.active = true
									}
								})
							}
							if (s.SUBJECT_TYPE == 9) {
								const {
									address,
									address_detail
								} = JSON.parse(list[0].answer_content)
								s.payload.address = address
								s.payload.address_detail = address_detail
							}
							if (s.SUBJECT_TYPE == 29) {
								s.payload.value = Number(list[0].answer_content)
							}

							if (s.SUBJECT_TYPE == 8) {
								s.payload.value = list[0].answer_content
							}

							if (s.SUBJECT_TYPE == 20) {
								list.forEach(item => {
									s.payload[item.option1].value = Number(item.option2)
								})
							}

							if (s.SUBJECT_TYPE == 30) {
								list.forEach(item => {
									s.payload[item.option1].value = Number(item.answer_content)
									s.options.find(o => o.ID == Number(item.option1)).nfs.forEach((n, index) => {
										if (index <= Number(item.answer_content)) {
											n.active = true
										}
									})
								})
							}

							if (s.SUBJECT_TYPE == 21) {
								list.forEach(item => {
									s.payload[item.option1].value.push(Number(item.option2))
								})
							}

							if (s.SUBJECT_TYPE == 31) {
								list.forEach(item => {
									s.payload[item.option1].value = Number(item.answer_content)
								})
							}
							if (s.SUBJECT_TYPE == 40) {
								s.options.forEach(o => {
									const item = list.find(l => l.option1 == o.ID)
									if (o.SHOW_TYPE === 10) {
										o.value = Number(item.option3.id)
									} else {
										o.value = Number(item.answer_content)
									}
									if (item.option_reason) {
										o.showReasonValue = true
										o.reasonValue = item.option_reason
									}
								})
							}
							if (s.SUBJECT_TYPE == 13) {
								s.payload.value = list[0].answer_content.split(',')
								s.payload.examPic = list[0].answer_content.split(',')
							}
						}
					}
				}
				this.updatePageSubjects(pageSubjects)
			},
			async wx_questionnaire_sel() {
				const {
					questionnaires,
					items
				} = await wx_questionnaire_sel({
					result_id: Number(this.question_id)
				})
				this.initData(questionnaires, items)
				this.$nextTick(() => {
					setTimeout(() => {
						this.loading = false
						this.setSubjectValue(items)
					}, 1000)
				})
			},
			getApi(cp, is_preview) {
				let apiName = 'wx_questionnaire_list'
				if (is_preview) {
					this[apiName](this.question_id, Number(is_preview))
				} else {
					this[apiName](this.question_id)
				}
			}
		},
		async onLoad(options) {
			const {
				scene,
				question_id,
				isDisabled,
				is_preview,
				cp,
				from_type,
				from_no
			} = options
			cp && (this.cp = true)
			from_type && (this.from_type = from_type)
			from_no && (this.from_no = from_no)
			this.loading = true
			this.question_id = Number(scene || question_id)
			this.getCurrentPages()
			if (isDisabled) {
				this.isDisabled = true
				this.wx_questionnaire_sel()
			}
			if (is_preview) {
				this.isPreview = true
			} else {
				this.isPreview = false
			}
			this.getApi(cp, is_preview)
			this.wx_get_questionnaire_advertising()
		}
	}
</script>

<style lang="scss" scoped>
	#navbar {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 1000;
	}

	#img-bg {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 640rpx;
		background: url(https://crm.fwh1988.cn/newimg/wenquan/wenjuanbg3.png) no-repeat center;
		z-index: 1;
	}

	.content {
		position: relative;
		z-index: 2;
	}

	#bg {
		width: 100%;
		padding-top: 208rpx;
		box-sizing: border-box;
	}

	.title {
		font-weight: 500;
		font-size: 48rpx;
		color: #000000;
		line-height: 66rpx;
		text-align: center;
		font-style: normal;
		padding: 0 98rpx;
	}

	.desc {
		font-weight: 400;
		font-size: 28rpx;
		color: #646566;
		line-height: 40rpx;
		text-align: center;
		font-style: normal;
		margin-top: 30rpx;
		padding: 0 32rpx;
	}

	.btn {
		background: #78FFE6;
		font-weight: 500;
		font-size: 32rpx;
		color: #FFF;
		line-height: 44rpx;
		letter-spacing: 1px;
		text-align: center;
		font-style: normal;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 48rpx;
		margin: 64rpx 24rpx 20rpx;
		height: 96rpx;
	}

	.plain-btn {
		background: #F7F8FA;
		font-weight: 500;
		font-size: 32rpx;
		color: #C6A36C;
		line-height: 44rpx;
		letter-spacing: 1px;
		text-align: center;
		font-style: normal;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 48rpx;
		margin: 0 24rpx;
		height: 96rpx;
	}

	.placeholder {
		height: 24rpx;
	}

	.submit {
		background: url(https://crm.fwh1988.cn/newimg/wenquan/wenjuanbg3.png);
		background-repeat: no-repeat;
		min-height: 1000rpx;
		background-size: contain;
		padding-top: 120rpx;


		&-evalute {
			font-weight: 500;
			font-size: 36rpx;
			color: #B88E53;
			line-height: 50rpx;
			font-style: normal;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		&-thank {
			font-weight: 400;
			font-size: 28rpx;
			color: #646566;
			line-height: 42rpx;
			text-align: center;
			font-style: normal;
			margin-top: 64rpx;
		}

		&-point {
			font-weight: 400;
			font-size: 28rpx;
			color: #646566;
			line-height: 42rpx;
			text-align: center;
			font-style: normal;
			margin-top: 16rpx;
		}

		&-back__home {
			margin: 116rpx 48rpx 32rpx;
			background: #78FFE6;
			border-radius: 48rpx;
			height: 96rpx;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFF;
			line-height: 44rpx;
			letter-spacing: 1px;
			text-align: center;
			font-style: normal;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		&-look__more {
			margin: 0 48rpx;
			height: 96rpx;
			border-radius: 48rpx;
			border: 2rpx solid #78FFE6;
			display: flex;
			justify-content: center;
			align-items: center;
			font-weight: 500;
			font-size: 32rpx;
			color: #78FFE6;
			line-height: 44rpx;
			text-align: center;
			font-style: normal;
		}
	}

	.noEvent {
		pointer-events: none;
	}
</style>